{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-08-18T07:23:11.894098Z",
     "start_time": "2025-08-18T07:23:11.234875Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:23:34.521374Z",
     "start_time": "2025-08-18T07:23:34.504215Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 通过series来创建\n",
    "s1 = pd.Series([1,2,3,4,5])\n",
    "s2 = pd.Series([6,7,8,9,10])\n",
    "df = pd.DataFrame({\"第1列\":s1,\"第2列\":s2})\n",
    "# 通过字典来创建\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"name\":[\"tom\",'jack','alice','bob','allen'],\n",
    "        \"age\":[15,17,20,26,30],\n",
    "        \"score\":[60.5,80,30.6,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=[\"name\",\"score\",\"age\"]\n",
    ")\n",
    "df"
   ],
   "id": "9e8447b1ac353795",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "1    tom   60.5   15\n",
       "2   jack   80.0   17\n",
       "3  alice   30.6   20\n",
       "4    bob   70.0   26\n",
       "5  allen   83.5   30"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bob</td>\n",
       "      <td>70.0</td>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>allen</td>\n",
       "      <td>83.5</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:23:53.013829Z",
     "start_time": "2025-08-18T07:23:53.009129Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# dataframe的属性\n",
    "print('行索引：')\n",
    "print(df.index)\n",
    "print('列标签：')\n",
    "print(df.columns)\n",
    "print('值')\n",
    "print(df.values)"
   ],
   "id": "eb83aeb5e40650a2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "行索引：\n",
      "Index([1, 2, 3, 4, 5], dtype='int64')\n",
      "列标签：\n",
      "Index(['name', 'score', 'age'], dtype='object')\n",
      "值\n",
      "[['tom' 60.5 15]\n",
      " ['jack' 80.0 17]\n",
      " ['alice' 30.6 20]\n",
      " ['bob' 70.0 26]\n",
      " ['allen' 83.5 30]]\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:24:10.298698Z",
     "start_time": "2025-08-18T07:24:10.294649Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print('维度：',df.ndim)\n",
    "print('形状:', df.shape)\n",
    "print('元素个数：', df.size)\n",
    "print('数据类型：')\n",
    "print(df.dtypes)"
   ],
   "id": "b0c2c9445a4e091f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "维度： 2\n",
      "形状: (5, 3)\n",
      "元素个数： 15\n",
      "数据类型：\n",
      "name      object\n",
      "score    float64\n",
      "age        int64\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:24:23.733560Z",
     "start_time": "2025-08-18T07:24:23.728991Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 行列转置\n",
    "print(df.T)"
   ],
   "id": "300dc95f66effada",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1     2      3     4      5\n",
      "name    tom  jack  alice   bob  allen\n",
      "score  60.5  80.0   30.6  70.0   83.5\n",
      "age      15    17     20    26     30\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:24:38.035170Z",
     "start_time": "2025-08-18T07:24:38.031375Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取元素 loc  iloc  at  iat\n",
    "# 某行\n",
    "print(df.loc[4])\n",
    "print(df.iloc[3])"
   ],
   "id": "85744cb718b53200",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name      bob\n",
      "score    70.0\n",
      "age        26\n",
      "Name: 4, dtype: object\n",
      "name      bob\n",
      "score    70.0\n",
      "age        26\n",
      "Name: 4, dtype: object\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:24:53.327935Z",
     "start_time": "2025-08-18T07:24:53.323389Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 某列\n",
    "print(df.loc[:,'name'])\n",
    "print(df.iloc[:,0])"
   ],
   "id": "d8d6b135461a90aa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n",
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:25:06.754433Z",
     "start_time": "2025-08-18T07:25:06.750703Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 单个元素\n",
    "print(df.at[3,'score'])\n",
    "print(df.iat[2,1])\n",
    "print(df.loc[3,'score'])\n",
    "print(df.iloc[2,1])"
   ],
   "id": "e4137b1ee4ae9ec4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.6\n",
      "30.6\n",
      "30.6\n",
      "30.6\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:25:47.449830Z",
     "start_time": "2025-08-18T07:25:47.441526Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取单列数据\n",
    "print(df['name'])\n",
    "print(type(df['name']))\n",
    "print(df.name)\n",
    "print(type(df.name))\n",
    "print(df[['name']])\n",
    "print(type(df[['name']]))\n",
    "df[['name']]"
   ],
   "id": "7085c49a24d70b9c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n",
      "<class 'pandas.core.series.Series'>\n",
      "1      tom\n",
      "2     jack\n",
      "3    alice\n",
      "4      bob\n",
      "5    allen\n",
      "Name: name, dtype: object\n",
      "<class 'pandas.core.series.Series'>\n",
      "    name\n",
      "1    tom\n",
      "2   jack\n",
      "3  alice\n",
      "4    bob\n",
      "5  allen\n",
      "<class 'pandas.core.frame.DataFrame'>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "    name\n",
       "1    tom\n",
       "2   jack\n",
       "3  alice\n",
       "4    bob\n",
       "5  allen"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bob</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>allen</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:26:11.389099Z",
     "start_time": "2025-08-18T07:26:11.384321Z"
    }
   },
   "cell_type": "code",
   "source": "print(df[['name','score']]) # 多列数据的获取",
   "id": "5889ef521dbe30bc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score\n",
      "1    tom   60.5\n",
      "2   jack   80.0\n",
      "3  alice   30.6\n",
      "4    bob   70.0\n",
      "5  allen   83.5\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:26:23.729347Z",
     "start_time": "2025-08-18T07:26:23.724770Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看部分数据\n",
    "print(df.head(2))"
   ],
   "id": "411c88654e4f220",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   name  score  age\n",
      "1   tom   60.5   15\n",
      "2  jack   80.0   17\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:26:35.298118Z",
     "start_time": "2025-08-18T07:26:35.293530Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.tail(3))",
   "id": "e1bf225c4b839f60",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "3  alice   30.6   20\n",
      "4    bob   70.0   26\n",
      "5  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:26:50.235421Z",
     "start_time": "2025-08-18T07:26:50.228605Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用布尔索引筛选数据\n",
    "df[df.score>70]\n",
    "df[ (df['score']>70) & (df.age<20)]"
   ],
   "id": "a83ba398b5ea4a27",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   name  score  age\n",
       "2  jack   80.0   17"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:27:13.590168Z",
     "start_time": "2025-08-18T07:27:13.583457Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 随机抽样\n",
    "df.sample(3)"
   ],
   "id": "5b7f2bbb43e3af80",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "1    tom   60.5   15\n",
       "3  alice   30.6   20\n",
       "2   jack   80.0   17"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>jack</td>\n",
       "      <td>80.0</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:27:27.052634Z",
     "start_time": "2025-08-18T07:27:27.048719Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"name\":[\"tom\",'jack','alice','bob','allen'],\n",
    "        \"age\":[15,17,20,26,30],\n",
    "        \"score\":[60.5,80,30.6,70,83.5]\n",
    "    },index=[1,2,3,4,5],columns=[\"name\",\"score\",\"age\"]\n",
    ")"
   ],
   "id": "7e21e2bc64cf933b",
   "outputs": [],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:27:39.352407Z",
     "start_time": "2025-08-18T07:27:39.345915Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.head()) #查看前n行数据，默认是5行\n",
    "print(df.tail(1))  #查看后n行数据，默认是5行"
   ],
   "id": "e2b91c2743c8c4ca",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "1    tom   60.5   15\n",
      "2   jack   80.0   17\n",
      "3  alice   30.6   20\n",
      "4    bob   70.0   26\n",
      "5  allen   83.5   30\n",
      "    name  score  age\n",
      "5  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:28:00.172506Z",
     "start_time": "2025-08-18T07:28:00.167868Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isin(['jack',20]))  #查看元素是否包含在参数集合中",
   "id": "9213896629b1e35d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score    age\n",
      "1  False  False  False\n",
      "2   True  False  False\n",
      "3  False  False   True\n",
      "4  False  False  False\n",
      "5  False  False  False\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:28:12.395501Z",
     "start_time": "2025-08-18T07:28:12.388992Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.isna()) # 查看元素是否是缺失值",
   "id": "816d6335315b52d0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score    age\n",
      "1  False  False  False\n",
      "2  False  False  False\n",
      "3  False  False  False\n",
      "4  False  False  False\n",
      "5  False  False  False\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:28:25.128176Z",
     "start_time": "2025-08-18T07:28:25.122815Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df['score'].sum())  #某一列的总和\n",
    "print(df.score.max())  #最大值\n",
    "print(df.age.min())  #最小值\n",
    "print(df.score.mean())  #平均数\n",
    "print(df.score.median())  #中位数\n",
    "print(df.age.mode())  #众数"
   ],
   "id": "743445f6708bdd87",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "324.6\n",
      "83.5\n",
      "15\n",
      "64.92\n",
      "70.0\n",
      "0    15\n",
      "1    17\n",
      "2    20\n",
      "3    26\n",
      "4    30\n",
      "Name: age, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:28:39.286162Z",
     "start_time": "2025-08-18T07:28:39.281425Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"name\":[\"tom\",\"tom\",'jack','alice','bob','allen'],\n",
    "        \"age\":[15,15,15,20,26,30],\n",
    "        \"score\":[60.5,60.5,80,30.6,70,83.5]\n",
    "    },index=[1,2,3,4,5,6],columns=[\"name\",\"score\",\"age\"]\n",
    ")"
   ],
   "id": "f4dd9dec2fb61eea",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:28:54.145003Z",
     "start_time": "2025-08-18T07:28:54.139975Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.score.std())  #标准差\n",
    "print(df.score.var()) #方差\n",
    "print(df.score.quantile(0.25))  #分位数"
   ],
   "id": "f7fac3e67ad86fb0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19.037375519400424\n",
      "362.4216666666666\n",
      "60.5\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:29:06.818067Z",
     "start_time": "2025-08-18T07:29:06.810579Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.describe())",
   "id": "bcf5e9f97425720c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "           score        age\n",
      "count   6.000000   6.000000\n",
      "mean   64.183333  20.166667\n",
      "std    19.037376   6.493587\n",
      "min    30.600000  15.000000\n",
      "25%    60.500000  15.000000\n",
      "50%    65.250000  17.500000\n",
      "75%    77.500000  24.500000\n",
      "max    83.500000  30.000000\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:29:17.988746Z",
     "start_time": "2025-08-18T07:29:17.984859Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.count())  #每一列非缺失值的个数",
   "id": "645125b039ad86e5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name     6\n",
      "score    6\n",
      "age      6\n",
      "dtype: int64\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:29:28.079589Z",
     "start_time": "2025-08-18T07:29:28.073967Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.value_counts()) #出现的次数",
   "id": "7074c7705919aa1b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name   score  age\n",
      "tom    60.5   15     2\n",
      "alice  30.6   20     1\n",
      "allen  83.5   30     1\n",
      "bob    70.0   26     1\n",
      "jack   80.0   15     1\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:29:40.967907Z",
     "start_time": "2025-08-18T07:29:40.963338Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.drop_duplicates())",
   "id": "f1666b0e95e6e5fd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "1    tom   60.5   15\n",
      "3   jack   80.0   15\n",
      "4  alice   30.6   20\n",
      "5    bob   70.0   26\n",
      "6  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:29:54.206718Z",
     "start_time": "2025-08-18T07:29:54.202846Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.duplicated(subset=['age']))  #查看是否重复",
   "id": "f462a170f3304489",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1    False\n",
      "2     True\n",
      "3     True\n",
      "4    False\n",
      "5    False\n",
      "6    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:30:05.028892Z",
     "start_time": "2025-08-18T07:30:05.022669Z"
    }
   },
   "cell_type": "code",
   "source": "df.sample(2) #随机抽样",
   "id": "892b7fb683eace67",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "4  alice   30.6   20\n",
       "2    tom   60.5   15"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:32:37.601004Z",
     "start_time": "2025-08-18T07:32:37.596100Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.replace(15,30))",
   "id": "7cc7d9e85250feb4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "1    tom   60.5   30\n",
      "2    tom   60.5   30\n",
      "3   jack   80.0   30\n",
      "4  alice   30.6   20\n",
      "5    bob   70.0   26\n",
      "6  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:32:49.438038Z",
     "start_time": "2025-08-18T07:32:49.431795Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.cumsum()\n",
    "df.cummin(axis=0)"
   ],
   "id": "13d509916bcb90bb",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "1    tom   60.5   15\n",
       "2    tom   60.5   15\n",
       "3   jack   60.5   15\n",
       "4  alice   30.6   15\n",
       "5  alice   30.6   15\n",
       "6  alice   30.6   15"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>jack</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:33:14.296791Z",
     "start_time": "2025-08-18T07:33:14.292511Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.sort_index(ascending=False))",
   "id": "7e1f78794f18dd07",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "6  allen   83.5   30\n",
      "5    bob   70.0   26\n",
      "4  alice   30.6   20\n",
      "3   jack   80.0   15\n",
      "2    tom   60.5   15\n",
      "1    tom   60.5   15\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:33:34.645032Z",
     "start_time": "2025-08-18T07:33:34.640024Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.sort_values(by='score'))",
   "id": "a24c41c429998617",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "4  alice   30.6   20\n",
      "1    tom   60.5   15\n",
      "2    tom   60.5   15\n",
      "5    bob   70.0   26\n",
      "3   jack   80.0   15\n",
      "6  allen   83.5   30\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:33:50.611916Z",
     "start_time": "2025-08-18T07:33:50.607640Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"name\":[\"tom\",\"tom\",'jack','alice','bob','allen'],\n",
    "        \"age\":[15,15,15,20,26,30],\n",
    "        \"score\":[60.5,60.5,80,30.6,70,80]\n",
    "    },index=[1,2,3,4,5,6],columns=[\"name\",\"score\",\"age\"]\n",
    ")"
   ],
   "id": "5572158185e31631",
   "outputs": [],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:34:00.364634Z",
     "start_time": "2025-08-18T07:34:00.359201Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.sort_values(by=['score','age'],ascending=[True,False]))",
   "id": "5c7cd66e9a0a2681",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    name  score  age\n",
      "4  alice   30.6   20\n",
      "1    tom   60.5   15\n",
      "2    tom   60.5   15\n",
      "5    bob   70.0   26\n",
      "6  allen   80.0   30\n",
      "3   jack   80.0   15\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:34:14.801091Z",
     "start_time": "2025-08-18T07:34:14.790924Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df.nlargest(2,columns=['score','age'])\n",
    "df.nsmallest(2,columns=['score','age'])"
   ],
   "id": "103d78920f5b055e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "    name  score  age\n",
       "4  alice   30.6   20\n",
       "1    tom   60.5   15"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>name</th>\n",
       "      <th>score</th>\n",
       "      <th>age</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>alice</td>\n",
       "      <td>30.6</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>tom</td>\n",
       "      <td>60.5</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:34:56.679175Z",
     "start_time": "2025-08-18T07:34:56.667763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "案例1：学生成绩分析\n",
    "场景：某班级的学生成绩数据如下，请完成以下任务：\n",
    "1. 计算每位学生的总分和平均分。\n",
    "2. 找出数学成绩高于90分或英语成绩高于85分的学生。\n",
    "3. 按总分从高到低排序，并输出前3名学生。\n",
    "'''\n",
    "import pandas as pd\n",
    "data = {\n",
    "    '姓名': ['张三', '李四', '王五', '赵六', '钱七'],\n",
    "    '数学': [85, 92, 78, 88, 95],\n",
    "    '英语': [90, 88, 85, 92, 80],\n",
    "    '物理': [75, 80, 88, 85, 90]\n",
    "}\n",
    "scores = pd.DataFrame(data)\n",
    "\n",
    "#1. 计算每位学生的总分和平均分。\n",
    "scores['总分'] = scores[['数学','英语','物理']].sum(axis=1)\n",
    "scores['平均分'] = scores['总分'] / 3\n",
    "scores['平均分2'] = scores[['数学','英语','物理']].mean(axis=1)\n",
    "#2. 找出数学成绩高于90分或英语成绩高于85分的学生。\n",
    "scores[ (scores['数学']>90 ) | (scores['英语']>85 )  ]\n",
    "#3. 按总分从高到低排序，并输出前3名学生。\n",
    "r1 = scores.sort_values('总分',ascending=False).head(3)\n",
    "r2 =scores.nlargest(3,columns=['总分'])\n",
    "print(r1)\n",
    "print(r2)"
   ],
   "id": "1962200ea2c1a9e5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   姓名  数学  英语  物理   总分        平均分       平均分2\n",
      "4  钱七  95  80  90  265  88.333333  88.333333\n",
      "3  赵六  88  92  85  265  88.333333  88.333333\n",
      "1  李四  92  88  80  260  86.666667  86.666667\n",
      "   姓名  数学  英语  物理   总分        平均分       平均分2\n",
      "3  赵六  88  92  85  265  88.333333  88.333333\n",
      "4  钱七  95  80  90  265  88.333333  88.333333\n",
      "1  李四  92  88  80  260  86.666667  86.666667\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:35:34.879415Z",
     "start_time": "2025-08-18T07:35:34.871350Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "案例2：销售数据分析\n",
    "场景：某公司销售数据如下，请完成以下任务：\n",
    "1. 计算每种产品的总销售额（销售额 = 单价 × 销量）。\n",
    "2. 找出销售额最高的产品。\n",
    "3. 按销售额从高到低排序，并输出所有产品信息。\n",
    "'''\n",
    "import pandas as pd\n",
    "\n",
    "data = {\n",
    "    '产品名称': ['A', 'B', 'C', 'D'],\n",
    "    '单价': [100, 150, 200, 120],\n",
    "    '销量': [50, 30, 20, 40]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "df['总销售额'] = df['单价']*df['销量']\n",
    "df.nlargest(1,columns=['总销售额'])\n",
    "df.sort_values('总销售额',ascending=False)"
   ],
   "id": "b79258ead61b55f9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "  产品名称   单价  销量  总销售额\n",
       "0    A  100  50  5000\n",
       "3    D  120  40  4800\n",
       "1    B  150  30  4500\n",
       "2    C  200  20  4000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>产品名称</th>\n",
       "      <th>单价</th>\n",
       "      <th>销量</th>\n",
       "      <th>总销售额</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>A</td>\n",
       "      <td>100</td>\n",
       "      <td>50</td>\n",
       "      <td>5000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>D</td>\n",
       "      <td>120</td>\n",
       "      <td>40</td>\n",
       "      <td>4800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>B</td>\n",
       "      <td>150</td>\n",
       "      <td>30</td>\n",
       "      <td>4500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>C</td>\n",
       "      <td>200</td>\n",
       "      <td>20</td>\n",
       "      <td>4000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:36:09.303838Z",
     "start_time": "2025-08-18T07:36:09.295118Z"
    }
   },
   "cell_type": "code",
   "source": [
    "'''\n",
    "案例3：电商用户行为分析\n",
    "场景：某电商平台的用户行为数据如下，请完成以下任务：\n",
    "1. 计算每位用户的总消费金额（消费金额 = 商品单价 × 购买数量）\n",
    "2. 找出消费金额最高的用户，并输出其所有信息\n",
    "3. 计算所有用户的平均消费金额（保留2位小数）\n",
    "4. 统计电子产品的总购买数量\n",
    "'''\n",
    "import pandas as pd\n",
    "\n",
    "data = {\n",
    "    '用户ID': [101, 102, 103, 104, 105],\n",
    "    '用户名': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],\n",
    "    '商品类别': ['电子产品', '服饰', '电子产品', '家居', '服饰'],\n",
    "    '商品单价': [1200, 300, 800, 150, 200],\n",
    "    '购买数量': [1, 3, 2, 5, 4]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "df['总消费金额'] = df['商品单价']*df['购买数量']\n",
    "df.nlargest(1,columns=['总消费金额'])\n",
    "df['总消费金额'].mean()\n",
    "df[df['商品类别']=='电子产品']['购买数量'].sum()"
   ],
   "id": "559c66c29dc74d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(3)"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "#数据变形\n",
    "import pandas as pd\n",
    "data = {\n",
    "    'ID': [1, 2],\n",
    "    'name':['张三','李四'],\n",
    "    'Math': [90, 85],\n",
    "    'English': [88, 92],\n",
    "    'Science': [95, 89]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "df\n",
    "df.T\n",
    "#宽表转长表\n",
    "df2= pd.melt(df, id_vars=['ID','name'], var_name='科目', value_name='分数')\n",
    "df2.sort_values(by=['name','科目'])\n",
    "#长表转宽表\n",
    "df3=pd.pivot(df2,index=['ID','name'],columns=['科目'],values='分数')\n",
    "#分列\n",
    "data = {\n",
    "    'ID': [1, 2],\n",
    "    'name':['alice smith','bob jack'],\n",
    "    'Math': [90, 85],\n",
    "    'English': [88, 92],\n",
    "    'Science': [95, 89]\n",
    "}\n",
    "df = pd.DataFrame(data)\n",
    "df[['first name','last name']] = df['name'].str.split(' ')\n",
    "# 加载数据\n",
    "df = pd.read_csv(\"data/sleep.csv\")\n",
    "\n",
    "df=df[['person_id','blood_pressure']]\n",
    "df[['high','low']]=df['blood_pressure'].str.split('/',expand=True)\n",
    "df"
   ],
   "id": "3039d09a96b41f3f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:37:10.946896Z",
     "start_time": "2025-08-18T07:37:10.933947Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 加载数据\n",
    "df_employees = pd.read_csv(\"data/employees.csv\")\n",
    "df_employees"
   ],
   "id": "297252bd3d3722a2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     employee_id first_name last_name     email  phone_number      job_id  \\\n",
       "0            100     Steven      King     SKING  515.123.4567     AD_PRES   \n",
       "1            101      N_ann   Kochhar  NKOCHHAR  515.123.4568       AD_VP   \n",
       "2            102        Lex   De Haan   LDEHAAN  515.123.4569       AD_VP   \n",
       "3            103  Alexander    Hunold   AHUNOLD  590.423.4567     IT_PROG   \n",
       "4            104      Bruce     Ernst    BERNST  590.423.4568     IT_PROG   \n",
       "..           ...        ...       ...       ...           ...         ...   \n",
       "102          202        Pat       Fay      PFAY  603.123.6666      MK_REP   \n",
       "103          203      Susan    Mavris   SMAVRIS  515.123.7777      HR_REP   \n",
       "104          204    Hermann      Baer     HBAER  515.123.8888      PR_REP   \n",
       "105          205    Shelley   Higgins  SHIGGINS  515.123.8080      AC_MGR   \n",
       "106          206    William     Gietz    WGIETZ  515.123.8181  AC_ACCOUNT   \n",
       "\n",
       "      salary  commission_pct  manager_id  department_id  \n",
       "0    24000.0             NaN         NaN           90.0  \n",
       "1    17000.0             NaN       100.0           90.0  \n",
       "2    17000.0             NaN       100.0           90.0  \n",
       "3     9000.0             NaN       102.0           60.0  \n",
       "4     6000.0             NaN       103.0           60.0  \n",
       "..       ...             ...         ...            ...  \n",
       "102   6000.0             NaN       201.0           20.0  \n",
       "103   6500.0             NaN       101.0           40.0  \n",
       "104  10000.0             NaN       101.0           70.0  \n",
       "105  12000.0             NaN       101.0          110.0  \n",
       "106   8300.0             NaN       205.0          110.0  \n",
       "\n",
       "[107 rows x 10 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>employee_id</th>\n",
       "      <th>first_name</th>\n",
       "      <th>last_name</th>\n",
       "      <th>email</th>\n",
       "      <th>phone_number</th>\n",
       "      <th>job_id</th>\n",
       "      <th>salary</th>\n",
       "      <th>commission_pct</th>\n",
       "      <th>manager_id</th>\n",
       "      <th>department_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>100</td>\n",
       "      <td>Steven</td>\n",
       "      <td>King</td>\n",
       "      <td>SKING</td>\n",
       "      <td>515.123.4567</td>\n",
       "      <td>AD_PRES</td>\n",
       "      <td>24000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>101</td>\n",
       "      <td>N_ann</td>\n",
       "      <td>Kochhar</td>\n",
       "      <td>NKOCHHAR</td>\n",
       "      <td>515.123.4568</td>\n",
       "      <td>AD_VP</td>\n",
       "      <td>17000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>102</td>\n",
       "      <td>Lex</td>\n",
       "      <td>De Haan</td>\n",
       "      <td>LDEHAAN</td>\n",
       "      <td>515.123.4569</td>\n",
       "      <td>AD_VP</td>\n",
       "      <td>17000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>100.0</td>\n",
       "      <td>90.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>103</td>\n",
       "      <td>Alexander</td>\n",
       "      <td>Hunold</td>\n",
       "      <td>AHUNOLD</td>\n",
       "      <td>590.423.4567</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>9000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>102.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>104</td>\n",
       "      <td>Bruce</td>\n",
       "      <td>Ernst</td>\n",
       "      <td>BERNST</td>\n",
       "      <td>590.423.4568</td>\n",
       "      <td>IT_PROG</td>\n",
       "      <td>6000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>103.0</td>\n",
       "      <td>60.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>102</th>\n",
       "      <td>202</td>\n",
       "      <td>Pat</td>\n",
       "      <td>Fay</td>\n",
       "      <td>PFAY</td>\n",
       "      <td>603.123.6666</td>\n",
       "      <td>MK_REP</td>\n",
       "      <td>6000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>201.0</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>103</th>\n",
       "      <td>203</td>\n",
       "      <td>Susan</td>\n",
       "      <td>Mavris</td>\n",
       "      <td>SMAVRIS</td>\n",
       "      <td>515.123.7777</td>\n",
       "      <td>HR_REP</td>\n",
       "      <td>6500.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>101.0</td>\n",
       "      <td>40.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>104</th>\n",
       "      <td>204</td>\n",
       "      <td>Hermann</td>\n",
       "      <td>Baer</td>\n",
       "      <td>HBAER</td>\n",
       "      <td>515.123.8888</td>\n",
       "      <td>PR_REP</td>\n",
       "      <td>10000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>101.0</td>\n",
       "      <td>70.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>105</th>\n",
       "      <td>205</td>\n",
       "      <td>Shelley</td>\n",
       "      <td>Higgins</td>\n",
       "      <td>SHIGGINS</td>\n",
       "      <td>515.123.8080</td>\n",
       "      <td>AC_MGR</td>\n",
       "      <td>12000.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>101.0</td>\n",
       "      <td>110.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>106</th>\n",
       "      <td>206</td>\n",
       "      <td>William</td>\n",
       "      <td>Gietz</td>\n",
       "      <td>WGIETZ</td>\n",
       "      <td>515.123.8181</td>\n",
       "      <td>AC_ACCOUNT</td>\n",
       "      <td>8300.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>205.0</td>\n",
       "      <td>110.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>107 rows × 10 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 38
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-18T07:37:40.090440Z",
     "start_time": "2025-08-18T07:37:40.081970Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 加载数据\n",
    "df_employees = pd.read_csv(\"data/employees.csv\")\n",
    "df_employees\n",
    "# 1. 将first_name首字母大写，其余小写\n",
    "df_employees['first_name'] = df_employees['first_name'].str.capitalize()\n",
    "\n",
    "# 2. 提取邮箱域名（@后部分）\n",
    "df_employees['email_domain'] = df_employees['email'].str.extract(r'@(.+)')\n",
    "\n",
    "print(df_employees[['first_name', 'email', 'email_domain']].head())"
   ],
   "id": "1e9336d3b4bbc7b7",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  first_name     email email_domain\n",
      "0     Steven     SKING          NaN\n",
      "1      N_ann  NKOCHHAR          NaN\n",
      "2        Lex   LDEHAAN          NaN\n",
      "3  Alexander   AHUNOLD          NaN\n",
      "4      Bruce    BERNST          NaN\n"
     ]
    }
   ],
   "execution_count": 39
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
